package com.hejing.初识异常;

import org.junit.Test;

/**
 * ClassName: ExceptionTest
 * Package: com.hejing.初识异常
 * Description:
 *
 * @Author: HeJing
 * @Create: 2023-04-07 23:57
 * @Version: 1.0
 */
public class ExceptionTest {
    
    // ************************ 运行时异常 *************************
    @Test
    public void test01(){
        int[] arr = new int[10];
        // System.out.println(arr[10]);  // ArrayIndexOutOfBoundsException
    }
    
    @Test
    public void test02(){
        String str = "hello";
        str = null;
        // System.out.println(str.toString());  // NullPointerException
    }

    @Test
    public void test03(){
        Object obj = new String();
        System.out.println("hello world");
        // Date date = (Date) obj;  // ClassCastException
    }

    @Test
    public void test04(){
        String str = "123";
        str = "abc";
        // int i = Integer.parseInt(str);  // NumberFormatException
        // System.out.println(i);
    }
    
    @Test
    public void test05(){
        int num = 10;
        // System.out.println(num / 0); // ArithmeticException
    }
    
    // ***************************** 编译时异常 **********************************

    /**
     * ClassNotFoundException
     */
    @Test
    public void test06(){
        // Class clz = Class.forName("java.lang.String");
    }

    /**
     *
     */
    @Test
    public void test07(){
       /* File file = new File("hello.txt");
        FileInputStream fis = new FileInputStream(file);  // FileNotFoundException(可能)

        int data = fis.read();  // IOException(可能)
        while(data != -1){
            System.out.println((char)data);
            data = fis.read();  // IOException(可能)
        }
        fis.close();  // IOException(可能)*/
    }
}

























